﻿using Authentication.Core.Data;
using Authentication.Model.Interface;
using Authentication.Model.Model;
using System;
using System.Data.Entity;
using System.Collections.Generic;
using System.Linq;
using Common.Contract.Authentication.Record;
using Common.Contract.SystemConfig.Record;
using System.Globalization;

namespace Authentication.Provider
{
    public class AccountProvider : GenericProvider, IUserProvider
    {
        public bool SaveLog(ActionLog log)
        {
            try
            {
                repository.ActionLogs.Add(log);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool SaveUserQuestion(UserQuestion record)
        {
            try
            {
                repository.UserQuestions.Add(record);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public List<DepartmentSme> RetrieveListDepartment()
        {
            var data = repository.DbContext as AuthenticationEntities;
            var query = from x in data.DepartmentSmes
                        select x;
            return query.ToList();
        }
        public Account RetrieveUser(string userName)
        {
            var data = repository.DbContext as AuthenticationEntities;
            var query = from x in data.Accounts.Include(x => x.Branch).Include(x => x.Group)
                        where x.UserName.ToUpper().Equals(userName.ToUpper())
                        select x;
            return query.FirstOrDefault();
        }
        public List<Account> RetrieveAllUser()
        {
            var data = repository.DbContext as AuthenticationEntities;
            var query = from x in data.Accounts
                        where !x.Dao.Equals("khong co DAO")
                        select x;
            return query.ToList();
        }
        public Account RetrieveUserByDao(string dao, UserRecord currentDao)
        {
            var data = repository.DbContext as AuthenticationEntities;
            var query = from x in data.Accounts.Include(x => x.Branch).Include(x => x.Group)
                        where x.Dao.ToUpper() == dao.ToUpper()
                        select x;
            switch (currentDao.GroupIdentifier)
            {
                case "1":
                    break;
                case "2":
                    query = query.Where(x => x.Branch.Region.Equals(currentDao.Zone));
                    break;
                case "3":
                    query = query.Where(x => x.Branch.BranchCodeSme.Equals(currentDao.BranchCode));
                    break;
                case "4":
                    query = query.Where(x => x.Dao.Equals(dao));
                    break;
                default:
                    break;
            }
            return query.FirstOrDefault();
        }
        public List<Account> RetrieveListUserByDao(out int totalRecord, UserRecord currentUser, FilterRecord textSearch = null, int pagesize = 0, int pageindex = 0)
        {
            var data = repository.DbContext as AuthenticationEntities;
            var query = from x in data.Accounts.Include(x => x.Branch)
                        where x.BranchCodeSme.Length == 3 && x.WorkingStatus == 1 && (x.Dao != "Không có mã DAO" && x.Dao != "khong co DAO")
                        select x;
            switch (currentUser.GroupIdentifier)
            {
                case "1":
                    break;
                case "2":
                    query = query.Where(x => x.Branch.Region.ToUpper().Equals(currentUser.Zone.ToUpper()));
                    break;
                case "3":
                    query = query.Where(x => x.Branch.BranchCodeSme.ToUpper().Equals(currentUser.BranchCode.ToUpper()));
                    switch (currentUser.Position)
                    {
                        case "MBOM":
                            query = query.Where(x => x.Position.Contains("MBO"));
                            break;
                        case "SBOM":
                            query = query.Where(x => x.Position.Contains("SBO"));
                            break;
                        case "TeamLead":
                            query = query.Where(x => x.Team.Equals(currentUser.Team));
                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    break;
            }
            if (textSearch != null)
            {
                if (!string.IsNullOrEmpty(textSearch.Region) && textSearch.Region.Trim() != "null")
                {
                    query = query.Where(x => textSearch.Region.ToUpper().Contains(x.Branch.Region.ToUpper()));
                }
                if (!string.IsNullOrEmpty(textSearch.Branch) && textSearch.Branch.Trim() != "null")
                {
                    query = query.Where(x => textSearch.Branch.Contains(x.Branch.BranchId));
                }
                if (!string.IsNullOrEmpty(textSearch.Dao) && textSearch.Dao.Trim() != "null")
                {
                    query = query.Where(x => textSearch.Dao.Contains(x.Dao));
                }
                if (!string.IsNullOrEmpty(textSearch.DaoName) && textSearch.DaoName.Trim() != "null")
                {
                    query = query.Where(x => textSearch.DaoName.Contains(x.Dao));
                }
                if (!string.IsNullOrEmpty(textSearch.ContractType))
                {
                    query = query.Where(x => x.PositionName.Contains(textSearch.ContractType));
                }
                if (!string.IsNullOrEmpty(textSearch.Email))
                {
                    query = query.Where(x => x.Email.Contains(textSearch.Email));
                }
                if (!string.IsNullOrEmpty(textSearch.CusOpenDate))
                {
                    List<string> listFilter = textSearch.CusOpenDate.Split('-').ToList();
                    DateTime FromDateCompare = DateTime.ParseExact(listFilter.FirstOrDefault().Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
                    DateTime ToDateCompare = DateTime.ParseExact(listFilter.LastOrDefault().Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
                    query = query.Where(x => x.DateSalesStart.Value >= FromDateCompare && x.DateSalesStart.Value <= ToDateCompare);
                }

            }
            totalRecord = query.Count();
            if (currentUser.Position.ToUpper().Equals("HUB MANAGER"))
            {
                query = query.OrderBy(x => new { x.Branch.Region, x.BranchCodeSme, x.Team, x.Dao });
            }
            else
            {
                query = query.OrderBy(x => new { x.Branch.Region, x.BranchCodeSme, x.Dao });
            }
            if (pagesize > 0 && pageindex >= 0)
            {
                query = query.Skip(pageindex * pagesize).Take(pagesize);
            }
            return query.ToList();
        }
        public DateBusiness RetrieveBusinessDate(string TableName)
        {
            var data = repository.DbContext as AuthenticationEntities;
            var query = from x in data.DateBusinesses
                        where x.NameTable.ToUpper().Equals(TableName.ToUpper())
                        select x;
            return query.FirstOrDefault();
        }
        public List<Account> CheckDaoAndBranch(List<string> listDao, UserRecord CurrentUser)
        {
            var data = repository.DbContext as AuthenticationEntities;
            var query = from x in data.Accounts
                        where x.BranchCodeSme.Equals(CurrentUser.BranchCode) && listDao.Contains(x.Dao)
                        select x;
            return query.ToList();
        }
    }
}
